package jdbc;

import java.sql.*;
import java.util.Scanner;

public class LoginTest2 {

    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        //1、通过反射加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2、建立连接
        Connection connection = DriverManager.
                getConnection("jdbc:mysql://master:3306/student",
                        "root",
                        "123456");

        //3、创建执行器
        String sql="select * from login where username=?";

        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ResultSet rs = ps.executeQuery();

        if (!rs.next()){
            System.out.println("没有此用户");
        }else {

            if (password != null && rs.getString("password").equals(password)) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败，密码错误");
            }
        }
        rs.close();
        ps.close();
        connection.close();

    }

}
